import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.common.exceptions import (
    NoSuchElementException,
    StaleElementReferenceException,
)
from icecream import ic

# 如果安装了webdriver-manager
from webdriver_manager.chrome import ChromeDriverManager


options = webdriver.ChromeOptions()
wd = webdriver.Chrome(service=Service(f".\\chromedriver_win136.exe"), options=options)
# 如果安装了webdriver-manager
# wd=webdriver.Chrome(service=Service(ChromeDriverManager().install()))
# 如果使用edge浏览器
# wd=webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()))
wd.get("https://pvp.qq.com/web201605/herolist.shtml")
wd.maximize_window()
time.sleep(5)
# 根据id查找页面元素，返回一个WebEelement
"""
try:
    element=wd.find_element(By.ID,"search")
    print(element)
    ic(element)
except NoSuchElementException as e:
    ic(e)
element.send_keys("扁鹊")
time.sleep(5)
element.send_keys("\n")
"""
try:
    element = wd.find_element(By.ID, "search")
except NoSuchElementException as e:
    ic(e)
time.sleep(1)
wd.execute_script("window.scrollTo(0,400)")
element.send_keys("孙")
time.sleep(1)
heros = wd.find_elements(By.TAG_NAME, "li")
time.sleep(5)
for h in heros:
    if "sunshangxiang" in (h.get_attribute("outerHTML")):
        h.click()


input()
wd.quit()
